package cn.devzyh.controller;

import cn.devzyh.domain.CourseLesson;
import cn.devzyh.domain.CourseSection;
import cn.devzyh.domain.ResultVo;
import cn.devzyh.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 课程章节和课时模块处理器
 */
@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;

    @GetMapping("/findSectionAndLesson")
    public ResultVo findSectionAndLesson(@RequestParam(value = "courseId", required = true) Integer courseId) {

        return new ResultVo(true, 200, "响应成功",
                courseContentService.findSectionAndLesson(courseId));
    }

    @PostMapping("/saveOrUpdateSection")
    public ResultVo saveOrUpdateSection(@RequestBody CourseSection section) {

        ResultVo res = new ResultVo(true, 200, "", null);
        if (null == section.getId()) {

            courseContentService.saveSection(section);
            res.setMessage("新增章节成功");
        } else {

            courseContentService.updateSection(section);
            res.setMessage("修改章节成功");
        }

        return res;
    }

    @GetMapping("/updateSectionStatus")
    public ResultVo updateSectionStatus(@RequestParam(value = "id", required = true) Integer id,
                                        @RequestParam(value = "status", required = true) Integer status) {

        courseContentService.updateSectionStatus(id, status);

        Map map = new HashMap();
        map.put("status", status);

        return new ResultVo(true, 200, "章节状态修改成功", map);
    }

    @PostMapping("/saveOrUpdateLesson")
    public ResultVo saveLesson(@RequestBody CourseLesson lesson) {

        ResultVo res = new ResultVo(true, 200, "", null);
        if (null == lesson.getId()) {

            courseContentService.saveLesson(lesson);
            res.setMessage("新增课时信息成功");
        } else {

            courseContentService.updateLesson(lesson);
            res.setMessage("修改课时信息成功");
        }

        return res;
    }

}